from pygame import Rect
from pygame import display as pdis
from pygame import Surface as ps

from libs.ZScreen import ZScreen


class ZNode ( object ) :
    def __init__ ( self , father , rect ) :
        super ( ZNode , self ).__init__ ( )
        self.father = father
        self.screen = pdis.get_surface ( )
        if not isinstance ( father , ZScreen ) :
            self.surface = self.screen
        else :
            self.surface = father.surface
        if isinstance ( rect , Rect ) :
            self.rect = rect
        else :
            self.rect = Rect ( rect )
        self.children = [ ]
        self.running = self.respose = True
        self._pos = self.rect.topleft
        self._size = self.rect.size
        self.image = ps ( self.rect.size )

    def draw ( self ) :
        if self.running :
            if self.children :
                for x in self.children :
                    x.draw ( )

    def update ( self , event , keys , m_keys , m_pos ) :
        if self.respose :
            for x in self.children :
                x.update ( event , keys , m_keys , m_pos )

    def add ( self , *nodes ) :
        if nodes :
            for x in nodes :
                self.children.append ( x )

    def remove ( self , node ) :
        self.children.remove ( node )

    def clear ( self ) :
        self.children = [ ]

    @property
    def _size ( self ) :
        return self.rect.size

    @_size.getter
    def _size ( self ) :
        return self.rect.size

    @_size.setter
    def _size ( self , size ) :
        self.rect.size = size

    @property
    def _pos ( self ) :
        return self.rect.topleft

    @_pos.getter
    def _pos ( self ) :
        return self.rect.topleft

    @_pos.setter
    def _pos ( self , pos ) :
        self.rect.topleft = pos
